import time

from django import forms
from mac import models
from mac.forms import bootstrapform
from django.forms import widgets as ws


class NoticeAddForm(bootstrapform.BootStrapForm, forms.ModelForm):
    mac_file = forms.FileField(label='csv文件上传', required=False)

    class Meta:
        model = models.Notice
        # fields = '__all__'
        fields = ['notice_number', 'consignee', 'delivery_department',
                  'phone', 'address', 'source', 'mac_file', 'mac_address',
                  'nums', 'remarks']
        widgets = {
            'notice_number': ws.TextInput(attrs={'value': time.strftime("%Y%m%d") + '-'})
        }


class NoticeEditForm(forms.ModelForm):
    mac_file = forms.FileField(label='csv文件上传', required=False)

    class Meta:
        model = models.Notice
        fields = ['notice_number', 'consignee', 'delivery_department',
                  'phone', 'address', 'source', 'mac_file', 'mac_address',
                  'nums', 'remarks']

    def __new__(cls, *args, **kwargs):
        for field_name in cls.base_fields:  # 字段都包含在了cls.base_fields中
            if not field_name == 'mac_file':
                filed_obj = cls.base_fields[field_name]
                # 添加属性
                filed_obj.widget.attrs.update({'class': 'form-control'})
        return forms.ModelForm.__new__(cls)

